<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control
      class="tab-control"
      ref="tabControl1"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      v-show="isTabFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control
        ref="tabControl2"
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
      />
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import HomeSwiper from "./compons/HomeSwiper"; //2.轮播图组件
import RecommendView from "./compons/RecommendView"; //3.商品推荐模块
import FeatureView from "./compons/FeatureView"; //4.商品特性模块

import NavBar from "components/common/navbar/NavBar"; //1.顶部导航信息栏
import TabControl from "components/content/tabcontrol/TabControl"; //5.tab栏控制模块
import GoodsList from "components/content/goods/GoodsList"; //6.单独产品展示模块
import Scroll from "components/common/scroll/Scroll"; //7.自定义滚动组件
import BackTop from "components/content/backtop/BackTop"; //8.返回到顶部组件
import { debounce } from "common/utils"; //9.工具模块
import { itemListenerMixin } from "common/mixin"; //10.组件之间的共同方法和数据

//首页轮播图数据请求
import { getHomeMultiData, getHomeGoods } from "network/home";

export default {
  name: "Home",
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,

    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  mixins: [itemListenerMixin], //混合到下面的mounted
  data() {
    return {
      banners: [],
      recommends: [],
      //'流行', '新款', '精选'数据保存
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop", //默认类型
      isShowBackTop: false, //默认backtop不显示
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  created() {
    //首次调用数据
    this.getHomeMultiData();

    //2-请求商品数量
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 20); //这里时间不能为0
    this.$refs.scroll.refresh();
  },
  deactivated() {
    //保存Y值
    this.saveY = this.$refs.scroll.getScrollY();

    //取消全局事件的监听,注意不能只传一个参数，这会导致关于这个事件的所有都会被取消
    this.$bus.$off("homeitemImageLoad", this.itemImgListenter);
  },
  methods: {
    /*
    事件监听
    */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    /*
    回到顶部的事件方法
    */
    backClick() {
      this.$refs.scroll.scrollTo(0, 0, 300);
    },
    //回到顶部消失的方法
    contentScroll(position) {
      this.isShowBackTop = -position.y > 1000;
      //决定tabcontrol是否吸顶
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    //上拉加载更多
    loadMore() {
      this.getHomeGoods(this.currentType);
      //加载更多的时候重新计算可滚动的的高度
    },
    swiperImageLoad() {
      // console.log(this.$refs.tabControl2.$el.offsetTop)
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
    /*
    网络请求首次获取数据方法
    */
    getHomeMultiData() {
      getHomeMultiData().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp(); //完成上拉加载更多
      });
    },
  },
};
</script>

<style scoped>
#home {
  /* position: relative; */
  height: 100vh;
  /* padding-top: 44px; */
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
}
.tab-control {
  position: relative;
  z-index: 9;
}
/* .home-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: var(--color-tint);
  color: #fff;
  z-index: 100;
} */
.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  right: 0;
  left: 0;
  overflow: hidden;
}
</style>
